<?php

namespace app\aiv110\controller;

use app\aiv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\aiv110\model\Order;
use app\aiv110\model\OrderYimei;

class YimeiController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 100000  
         * 100001  
         * 100002  
         * 100003  
         * 100004  
         * 100005  
         * 100006  
         */
    }

    /*
     * 丽人订单列表
     * author xfwed 
     * date 2020.10.19
     * 
     * @param int perPage 每页数量 非必须，默认20
     * @param int page 当前页数 非必须，默认0
     */

    public function yimeiOrderList() {
        $rule = [
            'type' => 'require',
            'ctm_id' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:0',
        ];
        $msg = [
            'type.require' => '类型必须',
            'ctm_id.require' => '用户id必须',
            'perPage.require' => '每页条数必须',
            'page.require' => '页数必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $type = $all['type'];//0全部1待付款2待服务3已完成4已取消
        $ctm_id = $all['ctm_id'];
       $perPage = isset($all['perPage']) ? $all['perPage'] : 20;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }

        $offset = ($page - 1) * $perPage;
        if($type == 1){
            $where['o.status'] = 1;
        }elseif ($type == 2) {
            $where['o.status'] = 2;
            $where['o.yimei_status'] = 0;
        }elseif ($type == 3) {
            $where['o.status'] = 2;
            $where['o.yimei_status'] = 1;
        }elseif ($type == 4) {
            $where['o.status'] = 0;
        }

        $where['o.type'] = 7;
        $where['o.ctm_id'] = $ctm_id;
        $where['o.userdel'] = 1;
        //100待付全款 110待付定金 120待付尾款 20待服务 21已完成
        $list = Order::alias('o')
                ->join("(SELECT * FROM ai_order_yimei WHERE id IN (SELECT MAX(id) FROM ai_order_yimei where ctm_id =".$ctm_id." GROUP by ord_id)) oy",'oy.ord_id = o.id','left')
                ->join('item i','i.id = oy.item_id','left')
                ->where($where)
                ->order('o.id desc')
                ->group('o.id')
                ->field('o.id,o.ord_num,o.status,o.pay_money,o.balance_money,o.yimei_status,o.paid_money,i.id item_id,i.item_name,i.item_img')
//                ->limit($offset, $perPage)
                ->paginate($perPage)
                ->each(function($item,$key){
                    $status = $item['status'];
                    $order_id = $item['id'];
                    if($status == 1){
                        $orderyimeiRow = OrderYimei::where('ord_id',$order_id)
                                ->order('id desc')
                                ->field('id,type,status,pay_money')
                                ->find();
                        if($orderyimeiRow['type']==0){
                            if($orderyimeiRow['status'] == 0 || $orderyimeiRow['status'] == 2){
                                $item['status'] = 100; //待付全款
                            }
                        }elseif ($orderyimeiRow['type']==1) {
                            if($orderyimeiRow['status'] == 0 || $orderyimeiRow['status'] == 2){
                                $item['status'] = 110; //待付定金
                                $item['pay_money'] = $orderyimeiRow['pay_money'];
                            }  else {
                                $item['status'] = 120; //待付尾款
                                $item['pay_money'] = $item['pay_money']-$item['paid_money'];
                            }
                        }elseif ($orderyimeiRow['type']==2) {
                            if($orderyimeiRow['status'] == 0 || $orderyimeiRow['status'] == 2){
                                $item['status'] = 120; //待付尾款
                                $item['pay_money'] = $item['pay_money']-$item['paid_money'];
                            }
                        }
                    }elseif ($status == 2) {
                        if($item['yimei_status'] == 0){
                            $item['status'] = 20;//待服务
                            $orderyimeiRow1 = OrderYimei::alias('o')
                                    ->join('item i','i.id = o.item_id','left')
                                    ->where('o.ord_id',$order_id)
                                    ->where('o.status',1)
                                    ->order('o.id desc')
                                    ->field('o.item_id,i.item_name,i.item_img')
                                    ->find();
                            $item['item_id'] = $orderyimeiRow1['item_id'];
                            $item['item_name'] = $orderyimeiRow1['item_name'];
                            $item['item_img'] = $orderyimeiRow1['item_img'];
                        }  else {
                            $item['status'] = 21;//已完成
                        }
                    }
                    $orderyimeilist = OrderYimei::where('ord_id',$order_id)
                                ->where('status',1)
                                ->order('id desc')
                                ->field('id,type,status,pay_money,prepay_id')
                                ->select();
                    $item['orderyimeilist'] = $orderyimeilist;
                    return $item;
                });
//                print_r($list->toArray());
        return Utils::ajaxReturn(0, '查询成功', $list->toArray()['data']);
    }
    
    /*
     * 丽人订单详情
     * author xfwed 
     * date 2020.10.20
     * 
     * @param int perPage 每页数量 非必须，默认20
     * @param int page 当前页数 非必须，默认0
     */

    public function yimeiOrderInfo() {
        $rule = [
            'ord_id' => 'require',
        ];
        $msg = [
            'ord_id.require' => '订单id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ord_id = $all['ord_id'];

        $where['o.type'] = 7;
        $where['o.id'] = $ord_id;
        //100待付全款 110待付定金 120待付尾款 20待服务 21已完成
        $orderRow = Order::alias('o')
                ->join("(SELECT * FROM ai_order_yimei WHERE id IN (SELECT MAX(id) FROM ai_order_yimei GROUP by ord_id)) oy",'oy.ord_id = o.id','left')
                ->join('item i','i.id = oy.item_id','left')
                ->join('store s','s.id = o.store_id','left')
                ->where($where)
                ->order('o.id desc')
                ->group('o.id')
                ->field('o.id,o.ord_num,o.status,o.pay_money,o.balance_money,o.yimei_status,o.paid_money,o.yimei_code,o.paid_money,i.id item_id,i.item_name,i.item_img,s.store_name,s.longitude,s.latitude,s.store_address')
                ->find();
        $status = $orderRow['status'];
        $order_id = $orderRow['id'];
//        $orderRow['pay_money'] = $orderRow['pay_money'] - $orderRow['paid_money'];
        if($status == 1){
            $orderyimeiRow = OrderYimei::where('ord_id',$ord_id)
                    ->order('id desc')
                    ->field('id,type,status,pay_money,ord_num,prepay_id')
                    ->find();
            $orderRow['yimei_ord_num'] = $orderyimeiRow['ord_num'];
            $orderRow['prepay_id'] = $orderyimeiRow['prepay_id'];
            if($orderyimeiRow['type']==0){
                if($orderyimeiRow['status'] == 0 || $orderyimeiRow['status'] == 2){
                    $orderRow['status'] = 100; //待付全款
                    $orderRow['obligation'] = $orderRow['pay_money'] - $orderRow['paid_money'];//待付金额
                }
            }elseif ($orderyimeiRow['type']==1) {
                if($orderyimeiRow['status'] == 0 || $orderyimeiRow['status'] == 2){
                    $orderRow['status'] = 110; //待付定金
                    $orderRow['obligation'] = Db::table('sys_dictionary')->where('type','YIMEI_ADVANCE_MONEY')->value('view');//待付金额
                }  else {
                    $orderRow['status'] = 120; //待付尾款
                    $orderRow['obligation'] = $orderRow['pay_money'] - $orderRow['paid_money'];//待付金额
                }
            }elseif ($orderyimeiRow['type']==2) {
                if($orderyimeiRow['status'] == 0 || $orderyimeiRow['status'] == 2){
                    $orderRow['status'] = 120; //待付尾款
                    $orderRow['obligation'] = $orderRow['pay_money'] - $orderRow['paid_money'];//待付金额
                }
            }
        }elseif ($status == 2) {
            if($orderRow['yimei_status'] == 0){
                $orderRow['status'] = 20;//待服务
                $orderyimeiRow1 = OrderYimei::alias('o')
                                    ->join('item i','i.id = o.item_id','left')
                                    ->where('o.ord_id',$order_id)
                                    ->where('o.status',1)
                                    ->order('o.id desc')
                                    ->field('o.item_id,i.item_name,i.item_img')
                                    ->find();
                            $orderRow['item_id'] = $orderyimeiRow1['item_id'];
                            $orderRow['item_name'] = $orderyimeiRow1['item_name'];
                            $orderRow['item_img'] = $orderyimeiRow1['item_img'];
            }  else {
                $orderRow['status'] = 21;//已完成
            }
        }
        $orderyimeilist = OrderYimei::where('ord_id',$ord_id)
                    ->where('status',1)
                    ->order('id asc')
                    ->field('id,type,status,pay_money,prepay_id,ord_num,add_time')
                    ->select();
        $orderRow['orderyimeilist'] = $orderyimeilist;
                
        return Utils::ajaxReturn(0, '查询成功', $orderRow);
    }

}
